let is_line = 0;

let hostRoot = '';

if (is_line == 1) {
	hostRoot = 'http://101.201.39.159:8000'
} else {
	hostRoot = 'http://localhost'
}
import {
	Base64
} from '../../commonjs/code.js'
var code = new Base64();
let date = new Date()
let year = date.getFullYear()
let month = date.getMonth() + 1;
let day = date.getDate()
let signNum = code.encode((year + month * 2 + day * 3) * 2)

function normalRequest(url, options = {}) {
	options.url = hostRoot+'/api/' + url;
	if (options.header == undefined) {
		options.header = {
			
			"Content-Type": "application/json"
		}
	}
	
	console.log(options);
	return new Promise((resolve, reject) => {
		if (url != 'gold/records' && url != 'gold/products') {
			uni.showLoading({
				title: '加载中'
			})
		}
		uni.request({
			url: options.url,
			method: options.method,
			data: options.data,
			header: options.header,
			success: ({
				data,
				statusCode,
				header
			}) => {
				if (statusCode >= 200 && statusCode < 300) {

				} else {
					if (statusCode === 401) {
						let token = uni.getStorageSync('token');
						let status_bar = uni.getStorageSync('status_bar');
						uni.clearStorageSync()
						if (data.message == '用户名或密码错误') {
							uni.showModal({
								title: '提示',
								content: '用户名或密码错误,请重新输入!',
								showCancel: false
							})
						}
						if (token != '' && status_bar != '') {
							needLogin.postMessage('needLogin')
						} else {
							if (url != 'auth') {
								uni.redirectTo({
									url: '/packs/pages/auth/auth?singleLogin=1'
								})
							}
						}
					}
					if (statusCode === 403) {
						uni.showModal({
							title: '提示',
							content: '请使用与App相同手机号登录小程序!',
							showCancel: false
						})
					}
					if (statusCode === 422) {
						const error = Object.values(data.errors)[0][0]
					}
					if (statusCode === 400 && url == 'withdraws/freelances') {
						uni.showModal({
							title: '提示',
							content: data.message,
							showCancel: false
						})
					}
					if (statusCode === 429) {
						uni.showModal({
							title: '提示',
							content: '请求过于频繁，请稍后再试',
							showCancel: false
						})
					}
					if (statusCode === 500) {
						uni.showModal({
							title: '提示',
							content: '服务器错误，请重试',
							showCancel: false
						})
					}
				}
				resolve(data)
			},
			fail: (err) => {
				reject(err)
			},
			complete: () => {
				uni.hideLoading()
			}
		})
	})
}

export default normalRequest
